# -*- coding: utf-8 -*-
import scrapy


class Git2Spider(scrapy.Spider):
    name = 'git2'
    allowed_domains = ['github.com']
    start_urls = ['https://github.com/login']

    def parse(self, response):
        # 从登陆界面中解析出post数据
        token = response.xpath('//input[@name="authenticity_token"]/@value').extract_first()
        post_data = {
            "commit": "Sign in",
            "authenticity_token": token,
            "login": "DrPandaWuDi",
            "password": "DrPandaWuDi6996",
            "webauthn-support" : "supported",
            "webauthn-iuvpaa-support" : "unsupported"
        }
        print(token)
        # 针对登录url发送post请求
        yield scrapy.FormRequest(
            url="https://github.com/session",
            formdata=post_data,
            callback=self.after_login
        )

    def after_login(self, response):
        yield scrapy.Request(
            url="https://github.com/DrPandaWuDi",
            callback=self.check_login
        )

    def check_login(self, response):
        print(response.xpath('/html/head/title/text()').extract_first())
